package com.xhx.accounting;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class DBHelper extends SQLiteOpenHelper {

    public static final String CREATE_Classification = "create table Classification(" +
            "id integer primary key autoincrement," +
            "classification_name text," +
            "classification_icon text," +
            "classification_direction integer)";

    public static final String CREATE_AccountBook = "create table AccountBook(" +
            "id integer primary key autoincrement," +
            "accountbook_name text,"+
            "isCurrentAccountBook integer)";

    public static final String CREATE_SavePlan = "create table SavePlan(" +
            "id integer primary key autoincrement," +
            "saveplan_name text," +
            "start_time integer," +
            "deadline integer," +
            "goal integer)";

    public static final String CREATE_Record = "create table Record(" +
            "id integer primary key autoincrement," +
            "timeStamp integer," +
            "classification integer," +
            "type integer," +
            "amount double," +
            "notes text," +
            "accountBookId integer)";

    public static final String CREATE_Budget = "create table Budget(" +
            "id integer primary key autoincrement," +
            "money double)";

    public static  final String CREATE_Saving = "create table Saving(" +
            "id integer primary key autoincrement," +
            "date integer," +
            "money integer," +
            "planId integer)";

    public static final String INSERT_Classification_1 = "insert into Classification(classification_name, classification_icon, classification_direction) values('餐饮','ic_food', -1)";
    public static final String INSERT_Classification_2 = "insert into Classification(classification_name, classification_icon, classification_direction) values('购物','ic_shopping', -1)";
    public static final String INSERT_Classification_3 = "insert into Classification(classification_name, classification_icon, classification_direction) values('交通','ic_transportation', -1)";
    public static final String INSERT_Classification_4 = "insert into Classification(classification_name, classification_icon, classification_direction) values('快递','ic_delivery', -1)";
    public static final String INSERT_Classification_5 = "insert into Classification(classification_name, classification_icon, classification_direction) values('礼物','ic_present', -1)";
    public static final String INSERT_Classification_6 = "insert into Classification(classification_name, classification_icon, classification_direction) values('零食','ic_snack', -1)";
    public static final String INSERT_Classification_7 = "insert into Classification(classification_name, classification_icon, classification_direction) values('日用','ic_daily', -1)";
    public static final String INSERT_Classification_8 = "insert into Classification(classification_name, classification_icon, classification_direction) values('水果','ic_fruit', -1)";
    public static final String INSERT_Classification_9 = "insert into Classification(classification_name, classification_icon, classification_direction) values('书籍','ic_book', -1)";
    public static final String INSERT_Classification_10 = "insert into Classification(classification_name, classification_icon, classification_direction) values('数码','ic_phone', -1)";
    public static final String INSERT_Classification_11 = "insert into Classification(classification_name, classification_icon, classification_direction) values('通讯','ic_communicate', -1)";
    public static final String INSERT_Classification_12 = "insert into Classification(classification_name, classification_icon, classification_direction) values('医疗','ic_medical', -1)";
    public static final String INSERT_Classification_13 = "insert into Classification(classification_name, classification_icon, classification_direction) values('娱乐','ic_recreation', -1)";
    public static final String INSERT_Classification_14 = "insert into Classification(classification_name, classification_icon, classification_direction) values('运动','ic_exercise', -1)";
    public static final String INSERT_Classification_15 = "insert into Classification(classification_name, classification_icon, classification_direction) values('住房','ic_house', -1)";
    public static final String INSERT_Classification_16 = "insert into Classification(classification_name, classification_icon, classification_direction) values('其他','ic_set', -1)";
    public static final String INSERT_Classification_17 = "insert into Classification(classification_name, classification_icon, classification_direction) values('工资','ic_salary', 1)";
    public static final String INSERT_Classification_18 = "insert into Classification(classification_name, classification_icon, classification_direction) values('兼职','ic_parttime', 1)";
    public static final String INSERT_Classification_19 = "insert into Classification(classification_name, classification_icon, classification_direction) values('理财','ic_money', 1)";
    public static final String INSERT_Classification_20 = "insert into Classification(classification_name, classification_icon, classification_direction) values('其他','ic_set', 1)";

    public static final String INSERT_AccountBook_1 = "insert into AccountBook(accountbook_name,isCurrentAccountBook) values('默认账本',1)";
    //public static final String INSERT_SavePlan_1 = "insert into SavePlan(saveplan_name) values('365天存钱')";

    private final Context mContext;

    public DBHelper(Context context) {
        super(context, "AccountingDB.db", null, 1);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_Saving);
        db.execSQL(CREATE_Budget);
        db.execSQL(CREATE_Record);
        db.execSQL(CREATE_SavePlan);
        db.execSQL(CREATE_AccountBook);
        db.execSQL(CREATE_Classification);
        db.execSQL(INSERT_Classification_1);
        db.execSQL(INSERT_Classification_2);
        db.execSQL(INSERT_Classification_3);
        db.execSQL(INSERT_Classification_4);
        db.execSQL(INSERT_Classification_5);
        db.execSQL(INSERT_Classification_6);
        db.execSQL(INSERT_Classification_7);
        db.execSQL(INSERT_Classification_8);
        db.execSQL(INSERT_Classification_9);
        db.execSQL(INSERT_Classification_10);
        db.execSQL(INSERT_Classification_11);
        db.execSQL(INSERT_Classification_12);
        db.execSQL(INSERT_Classification_13);
        db.execSQL(INSERT_Classification_14);
        db.execSQL(INSERT_Classification_15);
        db.execSQL(INSERT_Classification_16);
        db.execSQL(INSERT_Classification_17);
        db.execSQL(INSERT_Classification_18);
        db.execSQL(INSERT_Classification_19);
        db.execSQL(INSERT_Classification_20);
        db.execSQL(INSERT_AccountBook_1);
        db.execSQL("insert into Budget(money) values(2000)");
        //db.execSQL(INSERT_SavePlan_1);
        //
        Toast.makeText(mContext, "初始化成功", Toast.LENGTH_SHORT).show();
    }

    //数据库版本更新时发生改变会调用
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists Classification");
        db.execSQL("drop table if exists AccountBook");
        db.execSQL("drop table if exists Record");
        db.execSQL("drop table if exists SavePlan");
        onCreate(db);
    }
}
